package com.rescue.mapper;

import com.github.pagehelper.Page;
import com.rescue.forum.dto.ArticlePageQueryDTO;
import com.rescue.forum.dto.QueryArticleDTO;
import com.rescue.forum.dto.SearchDTO;
import com.rescue.forum.entity.ForumArticle;
import com.rescue.forum.entity.ForumArticleAttachment;
import com.rescue.forum.vo.AdminGetAttachmentVO;
import com.rescue.forum.vo.ArticlePageVO;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.time.LocalDate;
import java.util.List;

@Mapper
public interface ArticleMapper {
    Page<ArticlePageVO> page(ArticlePageQueryDTO articlePageQueryDTO);

    @Select("select * from forum_article_attachment where article_id = #{articleId}")
    List<AdminGetAttachmentVO> getFileByArticleId(String articleId);

    @Update("update forum_article set top_type = #{topType} where article_id = #{articleId}")
    void topArticle(String articleId, Integer topType);

    @Update("update forum_article set status = 1 where article_id = #{articleId}")
    void auditArticle(String articleId);

    @Update("update forum_article set comment_count = comment_count + #{deleteCount} where article_id = #{articleId}")
    void updateCommentCount(String articleId, Integer deleteCount);

    /**
     * 修改文章信息
     *
     * @param forumArticle
     */
    void update(ForumArticle forumArticle);

    @Delete("delete from forum_article where article_id = #{articleId}")
    void deleteByArticleId(String articleId);


    @Select("select * from forum_article where user_id = #{userId}")
    Page<ArticlePageVO> getByUserId(Integer userId);


    @Select("select * from forum_article where article_id in (select forum_comment.article_id from forum_comment where forum_article.user_id =#{userId})")
    Page<ArticlePageVO> getByUserComment(Integer userId);

    @Select("select * from forum_article where article_id in (select article_id from like_record where like_record.user_id = #{userId}) ")
    Page<ArticlePageVO> getByLike(Integer userId);

    /**
     * 发布文章
     *
     * @param article
     */
    void save(ForumArticle article);

    /**
     * @param queryArticleDTO
     * @return
     */
    Page<ArticlePageVO> list(QueryArticleDTO queryArticleDTO);

    @Select("select * from forum_article_attachment where article_id = #{articleId}")
    List<ForumArticleAttachment> userGetByArticleId(String articleId);

    @Select("select * from forum_article where article_id = #{articleId}")
    ForumArticle getById(String articleId);

    @Select("select * from forum_article where title like concat('%',#{keyword},'%') or content like concat('%',#{keyword},'%') or markdown_content like concat('%',#{keyword},'%')")
    Page<ArticlePageVO> search(SearchDTO searchDTO);

    @Select("select count(*) from forum_article where user_id = #{userId} and date(post_time) = #{date} ")
    Integer getArticleCount(LocalDate date, Integer userId);

    @Select("select count(*) from forum_article where user_id = #{userId}")
    Integer getNumById(Integer userId);
}
